package cn.haut.online_read.crawler;

import cn.haut.online_read.enrity.BookInfo;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.List;

/**
 * 在查询界面爬取书籍信息
 *
 * @author xing
 * @date 2021-04-25 15:00
 */
public class BookBySearchCrawler implements PageProcessor {

    private Site site = Site.me().setSleepTime(1);

    @Override
    public void process(Page page) {

        List<String> bookIdList = page.getHtml().xpath("//div[@class='book']/@data-href").replace("/book/", "").all();
        List<String> bookNameList = page.getHtml().xpath("//h4[@class='name']/a/text()").all();
        List<String> authorList = page.getHtml().xpath("//p[@class='author']/span[1]/text()").all();
        List<String> descList = page.getHtml().xpath("//p[@class='desc']/text()").all();
        List<String> imgUrlList = page.getHtml().xpath("//div[@class='book']/img/@src").all();
        List<String> popularityList = page.getHtml().xpath("//p[@class='popularity']/span[1]/text()").all();
        List<String> keepList = page.getHtml().xpath("//p[@class='popularity']/span[3]/text()").all();

        List<BookInfo> bookInfoList = BookInfo.getBookInfoList(bookIdList, bookNameList, authorList, descList, imgUrlList, keepList, popularityList);

        // 搜寻结果页数
        String pageNum = page.getHtml().xpath("//span[@class='total']/span/text()").get();

        // ref
        page.putField("books", bookInfoList);
        // ref
        page.putField("totalPages", pageNum);

    }

    @Override
    public Site getSite() {
        return site;
    }
}
